//
//  HJCancellationKaiPayErrorController.m
//  KaiChat
//
//  Created by lx on 2024/1/22.
//

#import "HJCancellationKaiPayErrorController.h"
#import "QMUITextField.h"
#import "QMUIButton.h"
#import "HJMinePresent.h"
#import "HJUserModel.h"
#import "HJIM.h"

@interface HJCancellationKaiPayErrorController ()<QMUITextFieldDelegate>

@property (nonatomic, weak) UIView *contentView;
@property (nonatomic, weak) UIView *maskView;
@property (nonatomic, weak) QMUITextField *textField;
@property (nonatomic, strong) NSString *inputContent;


@end

@implementation HJCancellationKaiPayErrorController


- (void)showInController:(UIViewController *)controller {
   [controller presentViewController:self animated:NO completion:nil];
}

 
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [IQKeyboardManager sharedManager].enable = NO;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = NO;

    _inputContent = @"I abandon my KaiPay assets and agree to their deletion";

    self.view.backgroundColor = UIColor.clearColor;
    
    UIButton *maskView = [[UIButton alloc] initWithFrame:self.view.bounds];
    maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    [maskView addTarget:self action:@selector(didClickMarkView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:maskView];
    
    UIView *contentView = [UIView addToView:maskView];
    contentView.backgroundColor = UIColor.whiteColor;
    contentView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, 338 + TabbarSafeBottomMargin);
    
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:contentView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(20, 20)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = contentView.bounds;
    maskLayer.path = bezierPath.CGPath;
    contentView.layer.mask = maskLayer;
    
    UIView *topView = [UIView addToView:contentView];
    topView.backgroundColor = UIColor.whiteColor;
    topView.frame = CGRectMake(0, 0, KScreenWidth, 50);
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(50);
        make.left.right.offset(0);
        make.top.offset(0);
    }];
    
    QMUIButton *closeButton = [QMUIButton addToView:topView];
    [closeButton addTarget:self action:@selector(didClickDismissButton) forControlEvents:UIControlEventTouchUpInside];
    [closeButton setImage:[UIImage imageNamed:@"nav_close"] forState:UIControlStateNormal];
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.width.height.offset(30);
        make.left.offset(leftRightMargin);
    }];
    
    UIImageView *iconImageView = [UIImageView addToView:contentView];
    iconImageView.image = [UIImage imageNamed:@"warn_icon"];
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(leftRightMargin);
        make.top.equalTo(topView.mas_bottom);
        make.width.height.offset(20);
    }];
    
    UILabel *titleLabel = [UILabel lableWithSuperView:contentView font:UIFontMediumMake(16) color:UIColor.tj_333333Color title:@"Important-Note" textAlignment:NSTextAlignmentLeft];
    titleLabel.numberOfLines = 0;
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(iconImageView);
        make.left.equalTo(iconImageView.mas_right).offset(7);
    }];
    
    UILabel *tipLabel = [UILabel lableWithSuperView:topView font:UIFontMake(12) color:UIColor.blackColor title:@"Please settle your KaiPay account assets before proceeding with the deletion.After deregistration, KaiPay assets cannot be recovered. Please be cautious." textAlignment:NSTextAlignmentLeft];
    tipLabel.attributedText = [tipLabel.text titleMargin:2 alignment:NSTextAlignmentLeft];
    tipLabel.numberOfLines = 0;
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconImageView.mas_bottom).offset(8);
        make.left.right.inset(leftRightMargin);
    }];
   
    UILabel *inputTipLabel = [UILabel lableWithSuperView:topView font:UIFontMediumMake(14) color:UIColor.tj_333333Color title:[NSString stringWithFormat:@"To abandon your assets, please enter:\n\"%@\"", _inputContent] textAlignment:NSTextAlignmentLeft];
    inputTipLabel.numberOfLines = 0;
    inputTipLabel.attributedText = [inputTipLabel.text attributedStringWithRangeString:[NSString stringWithFormat:@"\"%@\"", _inputContent] rangeColor:UIColor.tj_F4654DColor color:UIColor.tj_333333Color titleMargin:2];
    [inputTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tipLabel.mas_bottom).offset(8);
        make.left.right.inset(leftRightMargin);
    }];
    
    UIView *textFieldContentView = [UIView addToView:contentView];
    textFieldContentView.addCornerRadius(10);
    textFieldContentView.backgroundColor = UIColor.tj_F6F6F6Color;
    [textFieldContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.top.equalTo(inputTipLabel.mas_bottom).offset(10);
        make.height.offset(46);
    }];
    
    QMUITextField *textField = [QMUITextField addToView:textFieldContentView];
    textField.textColor = UIColor.tj_333333Color;
    textField.font = UIFontMake(14);
    textField.delegate = self;
    textField.placeholderColor = UIColor.tj_placeholderTextColor;
    textField.placeholder = @"Please enter";
    textField.backgroundColor = UIColor.tj_F6F6F6Color;
    textField.tintColor = UIColor.tj_mainColor;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.right.inset(10);
        make.top.bottom.offset(0);
    }];

    HJStyle2Button *abandonButton = [HJStyle2Button buttonWithSuperView:contentView font:UIFontMediumMake(14) color:nil title:@"Abandon Assets"];
    [abandonButton addTarget:self action:@selector(didClickAbandonButton) forControlEvents:UIControlEventTouchUpInside];
    [abandonButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.inset(leftRightMargin);
        make.right.equalTo(self.view.mas_centerX).inset(12);
        make.height.offset(55);
        make.bottom.inset(10 + TabbarSafeBottomMargin);
    }];
    
    HJStyle1Button *OKButton = [HJStyle1Button buttonWithSuperView:contentView font:UIFontMediumMake(14) color:nil title:@"Take action"];
    [OKButton addTarget:self action:@selector(didClickOKButton) forControlEvents:UIControlEventTouchUpInside];
    [OKButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(abandonButton);
        make.right.inset(leftRightMargin);
        make.left.equalTo(self.view.mas_centerX).inset(12);
        make.bottom.equalTo(abandonButton);
    }];
    
    _textField = textField;
    _maskView = maskView;
    _contentView = contentView;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlerKeyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlerKeyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)didClickAbandonButton {
    if (![_textField.text.lowercaseString isEqualToString:_inputContent.lowercaseString]) {
        [HJHUD showErrorMessage:@"Incorrect input for verification"];
        return;
    }

    [self dismissComplete:^{
        if (self.selectComplete) {
            self.selectComplete(YES);
        }
    }];
}

- (void)didClickDismissButton {
    [self.textField resignFirstResponder];
    [self dismissComplete:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.contentView.tj_y = KScreenHeight - self.contentView.tj_height;
        self.maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    } completion:^(BOOL finished) {
    }];
    
}

- (void)dismissComplete:(void (^)(void))complete {
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
        self.contentView.tj_y = KScreenHeight;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (complete) {
                complete();
            }
            [IQKeyboardManager sharedManager].enable = YES;
            [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
        }];
    }];
}

- (void)didClickMarkView {
    [self.textField resignFirstResponder];

}
 
- (void)didClickOKButton {
    [self dismissComplete:^{
        if (self.selectComplete) {
            self.selectComplete(NO);
        }
    }];
}

- (void)handlerKeyboardWillShowNotification:(NSNotification *)noti {
    
    if (!self.textField.isFirstResponder) {
        return;
    }
        
    NSDictionary *userInfo = [noti userInfo];
    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGFloat height = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    
    [UIView setAnimationCurve:curve];
    [UIView animateWithDuration:duration animations:^{
        CGRect rect = self.contentView.frame;
        rect.origin.y = KScreenHeight- (self.contentView.frame.size.height + height) + TabbarSafeBottomMargin;
        self.contentView.frame = rect;
        self.maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    }];
}

- (void)handlerKeyboardWillHideNotification:(NSNotification *)noti {

    if (!self.textField.isFirstResponder) {
        return;
    }
 
    NSDictionary *userInfo = [noti userInfo];
    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];//动画曲线类型
    [UIView setAnimationCurve:curve];
    [UIView animateWithDuration:duration animations:^{
        CGRect rect = self.contentView.frame;
        rect.origin.y = KScreenHeight - self.contentView.tj_height;
        self.contentView.frame = rect;
    } completion:^(BOOL finished) {

    }];
}
 
- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationCustom;
}


- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}




@end
